package com.ecmap.hongyun.commons.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ecmap.hongyun.model.User;


/**
 * 
 * @className:UserUtil.java
 * @classDescription:用户工具
 * @author: DANIEL DENG
 * @createTime: 2013-4-24
 *
 */
public class UserUtil {
	
	/**
	 * 从session中获取用户
	 * @param request Http请求对象
	 * @return 用户对象
	 */
	public static User getUserFromSession(HttpServletRequest request){
		Object user = request.getSession().getAttribute(Constants.SESSION_OPERATOR);
		return user == null ? null:(User)user;
	}
	
	/**
	 * 保存用户到session
	 * @param request Http请求对象
	 * @param user 用户对象
	 */
	public static void setUserInSession(HttpServletRequest request,User user){
		request.getSession().setAttribute( Constants.SESSION_OPERATOR , user);
	}
	
	/**
	 * 保存用户到cookie，用于用户自动登录只用，仅当ID与账号一致的情况下才允许自动登录
	 * @param response Http响应对象
	 * @param user 用户对象
	 */
	public static void setUserInCookie(HttpServletResponse response,User user){
		//保存对象ID
//    	Cookie cookie = new Cookie("i",operator.getOperatorId().toString());
//    	cookie.setPath("/");
//    	cookie.setMaxAge(Integer.MAX_VALUE);
//    	response.addCookie(cookie);
//    	//保存账号md5
//    	cookie = new Cookie("n", CommonUtil.getMD5( operator.getAccount() ) );
//    	cookie.setPath("/");
//    	cookie.setMaxAge(Integer.MAX_VALUE);
//    	response.addCookie(cookie);
	}
	
	/**
	 * 从cookie中获取cookie信息，校验对象ID与md5后的账号是否一致，匹配的话就返回操作者对象，否则返回null
	 * @param request Http请求对象
	 */
	public static User getUserFormCookie(HttpServletRequest request){
//		String operatorId = null ;
//		String account = null ;
//		Cookie[] cookies = request.getCookies();
//		try{
//			
//			for( Cookie cookie : cookies ){
//				if( "i".equals( cookie.getName() ) ){
//					operatorId = cookie.getValue();
//				}else if( "n".equals( cookie.getName() ) ){
//					account = cookie.getValue();
//				}
//			}
//			
//			//这两参数不可以为空
//			if( StringUtils.isNotEmpty(operatorId) && StringUtils.isNotEmpty(account) ){
//				try {
//					Operator opt = UserUtil.findOperatorByOperatorId(operatorId);
//					
//					if( account.equalsIgnoreCase(CommonUtil.getMD5(  opt.getAccount()) ) ){
//						//完全匹配
//						return opt;
//					}else{
//						return null;
//					}
//				} catch (Exception e) {
//					return null;
//				}
//			}
//		}catch(Exception e){
//			
//		}
//
//		
		
		return null;
	}
	
	
	/**
	 * 退出登录
	 * @param request
	 */
	public static void logout(HttpServletRequest request,HttpServletResponse response){
		HttpSession session = request.getSession();
		session.removeAttribute(Constants.SESSION_OPERATOR);
		session.invalidate();
		
		Cookie[] cookies=request.getCookies(); 
		try{ 
		     for(int i=0;i<cookies.length;i++){ 
			      Cookie cookie =	 cookies[i];
			      cookie.setMaxAge(0); 
			      cookie.setPath("/");//根据你创建cookie的路径进行填写     
			      response.addCookie(cookie); 
		     } 
		}catch(Exception ex){ 
		} 
	}
	
	


}
